package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.client.StationClient;
import com.woniu.client.dto.StationDto;
import com.woniu.exception.RebornException;
import com.woniu.mapper.MonthTotalMapper;
import com.woniu.model.MonthTotal;
import com.woniu.model.Result;
import com.woniu.service.MonthTotalService;
import com.woniu.service.param.FindByStationIdAndMonthPageParam;
import com.woniu.service.param.FindByStationIdAndMonthParam;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.Month;
import java.time.Year;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@Service
public class MonthTotalServiceImpl extends ServiceImpl<MonthTotalMapper, MonthTotal> implements MonthTotalService {

    @Resource
    private MonthTotalMapper monthTotalMapper;
    @Resource
    private StationClient stationClient;

    /**
     * 根据站点id和月份查找
     * id:站点id
     */
    @Override
    public List<MonthTotal> findByStationIdAndMonth(FindByStationIdAndMonthParam param) {
        Result<StationDto> byStationId = stationClient.findByStationId(param.getStationId());
        if (byStationId.getCode()!=200){
            throw new RebornException(byStationId.getMessage(),byStationId.getCode());
        }
        StationDto data = byStationId.getData();
        if (data==null) {
            throw new RebornException("不存在的站点",60006);
        }
        QueryWrapper<MonthTotal> wrapper = new QueryWrapper<>();
        wrapper.eq("station_id",param.getStationId());
        wrapper.eq("year",param.getYear());
        wrapper.eq("month",param.getMonth());
        List<MonthTotal> monthTotals = monthTotalMapper.selectList(wrapper);
        if (monthTotals==null||monthTotals.isEmpty()){
            throw new RebornException("选中的月份暂无任何统计数据",60004);
        }
        return monthTotals;
    }
    /**
     * 根据月份获取(默认当年)
     * id:站点id
     */
    @Override
    public List<MonthTotal> findByMonth(Integer id) {
        // 获取当前年份
        int currentYear = java.time.Year.now().getValue();

        QueryWrapper<MonthTotal> wrapper = new QueryWrapper<>();
        wrapper.eq("year", currentYear);  // 设置年份条件
        wrapper.eq("month", id);          // 设置月份条件
        List<MonthTotal> monthTotals = monthTotalMapper.selectList(wrapper);
        if (monthTotals == null || monthTotals.isEmpty()) {
            throw new RebornException("选中的月份暂无任何统计数据", 60004);
        }
        return monthTotals;
    }

    @Override
    public Page<MonthTotal> findByStationIdAndMonthPage(FindByStationIdAndMonthPageParam param) {
        int pageNum = param.getPageNum() == null ? 1 : param.getPageNum();
        int pageSize = param.getPageSize() == null ? 3 : param.getPageSize();

        // 获取站点信息
        Result<StationDto> byStationId = stationClient.findByStationId(param.getStationId());
        if (byStationId.getCode() != 200) {
            throw new RebornException(byStationId.getMessage(), byStationId.getCode());
        }

        StationDto data = byStationId.getData();
        if (data == null) {
            throw new RebornException("不存在的站点", 60006);
        }

        // 获取当前年份和月份
        LocalDate currentDate = LocalDate.now();
        int yearValue = currentDate.getYear();
        int monthValue = currentDate.getMonthValue();

        // 规定年份和月份
        int year = param.getYear() == 0 ? yearValue : param.getYear();
        int month = param.getMonth() == 0 ? monthValue : param.getMonth();

        // 构建查询条件
        QueryWrapper<MonthTotal> wrapper = new QueryWrapper<>();
        wrapper.eq("station_id", param.getStationId());
        wrapper.eq("year", year);
        wrapper.eq("month", month);

        // 分页查询
        Page<MonthTotal> monthTotalPage = new Page<>(pageNum, pageSize);
        Page<MonthTotal> monthTotals = monthTotalMapper.selectPage(monthTotalPage, wrapper);

        if (monthTotals == null || monthTotals.getRecords().isEmpty()) {
            throw new RebornException("选中的月份暂无任何统计数据", 60004);
        }
        return monthTotals;
    }

    @Override
    public Page<MonthTotal> findByMonthPage(Integer id, Integer pageNum, Integer pageSize) {
        pageNum =pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 3 : pageSize;
        Result<StationDto> byStationId = stationClient.findByStationId(id);
        if (byStationId.getCode()!=200){
            throw new RebornException(byStationId.getMessage(),byStationId.getCode());
        }
        StationDto data = byStationId.getData();
        if (data==null) {
            throw new RebornException("不存在的站点",60006);
        }
        // 获取当前年份
        int currentYear = java.time.Year.now().getValue();
        Page<MonthTotal> monthTotalPage = new Page<>(pageNum, pageSize);
        QueryWrapper<MonthTotal> wrapper = new QueryWrapper<>();
        wrapper.eq("year", currentYear);  // 设置年份条件
        wrapper.eq("month", id);          // 设置月份条件
        Page<MonthTotal> monthTotalPage1 = monthTotalMapper.selectPage(monthTotalPage, wrapper);
        if (monthTotalPage1.getRecords() == null || monthTotalPage1.getRecords().isEmpty()) {
            throw new RebornException("选中的月份暂无任何统计数据", 60004);
        }
        return monthTotalPage1;
    }


}
